//
//  DYShopListVC.m
//  TerritoryShopkeeper
//
//  Created by 黄德玉 on 2018/7/19.
//  Copyright © 2018年 ZDQK. All rights reserved.
//

#import "DYShopListVC.h"
#import "DYHomeShopListCell.h"
#import "DYShopDetailVC.h"
#import <Masonry/Masonry.h>


@interface DYShopListVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic,assign) NSInteger pageNum;

@property (nonatomic,strong) NSMutableArray * shops;

@end

@implementation DYShopListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"店铺";
    _tableView = [[UITableView alloc] init];
    [_tableView registerClass:[DYHomeShopListCell class] forCellReuseIdentifier:@"cell"];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    _tableView.estimatedRowHeight = 200;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pageNum = 1;
        [weakSelf _loadData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.pageNum++;
        [weakSelf _loadData];
    }];
    self.pageNum = 1;
    self.shops = [NSMutableArray array];
    [self _loadData];
}

- (void)_loadData{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters safeSetObject:@(self.pageNum) forKey:@"pageNumber"];
    [parameters safeSetObject:@10 forKey:@"pageSize"];
    __weak typeof(self) weakSelf = self;
    [MYAFNetworking POST:@"api2/shoplist" parameters:parameters  success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if ([[responseObject safeStringForKey:@"code"] isEqualToString:@"200"]) {
            id data = [responseObject safeObjectForKey:@"data"];
            DYShopListModel * listModel = [DYShopListModel mj_objectWithKeyValues:data];
            if (listModel.firstPage) {
                self.shops = [listModel.list mutableCopy];
            }else{
                [self.shops addObjectsFromArray:listModel.list];
            }
            if (listModel.lastPage) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showError:weakSelf.view title:@"" code:[responseObject safeStringForKey:@"code"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.shops.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DYHomeShopListCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    DYShopModel * model = [DYShopModel mj_objectWithKeyValues:self.shops[indexPath.row]];
    [cell loadData:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DYShopDetailVC * vc = [[DYShopDetailVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    vc.shopInfo = self.shops[indexPath.row];
}

@end
